using System.Collections.ObjectModel;
using HMCollada.Core;
using System.Xml;

namespace HMCollada.Core {
    struct HMColladaPolygons {
        internal int count;
        internal string material;
        internal string name;
        internal Collection<HMColladaInput> input;
        internal Collection<int[]> p;
        internal Collection<HMColladaPH> ph;
    }
}

namespace HMCollada {
    internal partial class HMColladaLoader {
        private static HMColladaPolygons LoadPolygons() {
            HMColladaPolygons newPolygons = new HMColladaPolygons();
            newPolygons.count = int.Parse(colladaReader.GetAttribute("count"));
            newPolygons.material = colladaReader.GetAttribute("material");
            newPolygons.name = colladaReader.GetAttribute("name");

            newPolygons.input = new Collection<HMColladaInput>();
            newPolygons.p = new Collection<int[]>();
            newPolygons.ph = new Collection<HMColladaPH>();

            if (!colladaReader.IsEmptyElement) colladaReader.Read();
            do {
                if (colladaReader.NodeType != XmlNodeType.EndElement) {
                    switch (colladaReader.Name) {
                        case "input": newPolygons.input.Add(LoadInput()); break;
                        case "p":
                            string content = ReadString().Trim();
                            while (content.Contains("  ")) content = content.Replace("  ", " ");
                            string[] values = content.Split(new char[] { ' ' });
                            int[] p = new int[values.Length];
                            for (int i = 0; i < p.Length; i++) p[i] = int.Parse(values[i]);
                            newPolygons.p.Add(p);
                            break;

                        case "ph": newPolygons.ph.Add(LoadPH()); break;
                    }
                }

                if (!colladaReader.IsEmptyElement) colladaReader.Read();
            } while (colladaReader.Name != "polygons");

            if (colladaReader.IsEmptyElement) colladaReader.Read();
            return newPolygons;
        }
    }
}